package com.bbs.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.bbs.core.common.WebUtils;
import com.bbs.core.model.Message;
import com.bbs.core.model.Topic;
import com.bbs.core.model.User;
import com.bbs.core.service.MessageService;
import com.bbs.core.service.TopicService;

@Controller
public class MessageController {
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private WebUtils webUtils;
	
	@Autowired
	private TopicService topicService;
	
	@RequestMapping("/bbs/myMessage.html")
	public ModelAndView  myPage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView view = new ModelAndView();
		view.setViewName("/message.html");
		User user = webUtils.currentUser(request, response);
		List<Topic> list = topicService.queryMyMessageTopic(user.getId());
		view.addObject("list", list);
		return view;
	}
	
	@RequestMapping("/bbs/my/{p}.html")
	public RedirectView  openMyTopic(@PathVariable int p,HttpServletRequest request, HttpServletResponse response){
		User user = webUtils.currentUser(request, response);
		Message message = messageService.makeOneBbsMessage(user.getId(), p,(byte) 0);
		messageService.updateMyTopic(message.getId(), (byte) 0);
		return  new RedirectView( request.getContextPath()+"/bbs/topic/"+p+"-1.html");
	}

}
